﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DTOSingTowerMall;
using FacadeSingTowerMall;

namespace WebFormSingTowerMall.Account
{
    public partial class UpdateAccount : System.Web.UI.Page
    {
        MembershipUser CurrentUser;
        UserAccountFacade AcctFacade;
        IUserDetail userDetail;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
            AcctFacade = new UserAccountFacade();
            
            if (!IsPostBack)
            {
                userDetail = AcctFacade.GetUserDetail((Guid)CurrentUser.ProviderUserKey);
                if (userDetail != null)
                {
                    lblName.Text = userDetail.FullName;
                    FullName.Text = userDetail.FullName;

                    if (userDetail.Gender == "M") MaleRadio.Checked = true;
                    else MaleRadio.Checked = false;
                    if (userDetail.Gender == "F") FemaleRadio.Checked = true;
                    else FemaleRadio.Checked = false;

                    ContactNo.Text = userDetail.ContactNo;
                    DateofBirth.Text = userDetail.DateofBirth.ToString();
                    Address.Text = userDetail.Address;
                }
            }

        }

        protected void btnCalendar_Click(object sender, EventArgs e)
        {
            DateofBirthCalendar.Visible = true;
        }

        protected void DateofBirthCalendar_SelectionChanged(object sender, EventArgs e)
        {
            DateofBirth.Text = DateofBirthCalendar.SelectedDate.ToShortDateString();
            DateofBirthCalendar.Visible = false;
        }

        protected void btnSubmitDetailChange_Click(object sender, EventArgs e)
        {
            string Gender = " ";
            if (MaleRadio.Checked) Gender = "M";
            if (FemaleRadio.Checked) Gender = "F";

            AcctFacade.UpdateUserAccount((Guid)CurrentUser.ProviderUserKey, FullName.Text, Gender, ContactNo.Text, DateofBirth.Text, Address.Text);

            if (AcctFacade.ProcessSuccess)
            {
                lblUpdateDetail.Text = AcctFacade.Message;
            }
            else
            {
                CustomValidator val = new CustomValidator();
                val.IsValid = false;
                val.ErrorMessage = AcctFacade.Message;
                this.Page.Validators.Add(val);
            }
        }
    }
}